import jieba

filename = "C:/Users/Administrator/Desktop/test.txt"
output_file = "C:/Users/Administrator/Desktop/output.txt"

# 读取文件内容并进行分词
with open(filename, encoding="utf-8") as f:
    content = f.read()
words = list(jieba.cut(content))

# 过滤停用词
stop_words = ["，", "。", "！", "？", "、", "；", "：", "“", "”", "（", "）", "【", "】"]
words = [word for word in words if word not in stop_words]

# 统计词频
word_count = {}
for word in words:
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1

# 获取出现次数前5的词汇和出现次数
top_5 = sorted(word_count.items(), key=lambda item: item[1], reverse=True)[:5]

# 写入到output.txt文件中
with open(output_file, mode="w", encoding="utf-8") as f:
    for pair in top_5:
        f.write(f"{pair[0]}: {pair[1]}\n")
